<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2019-05-30
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-27 00:18:33
 */

namespace app\admin\controller;
use app\common\controller\AdminController;

class SystemUser extends AdminController {
	
    protected function initialize(): void {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '账号管理', '系统', 'systemUser');

		$this->userModel = model('SystemUser', 'admin');

		//获取角色组
		$this->groupModel = model('SystemGroup', 'admin');
		list($groupList) = $this->groupModel->search([]);
		$this->assign('groupList', $groupList);
		
		$user_state_config = $this->userModel->get_user_state_config();
		$this->assign('user_state_config', $user_state_config);
	}

	public function index() {
		list($map, $parameter) = $this->getMap();

		$count = $this->userModel->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->userModel->search($map, $Page->getLimit());
		//print_r($list);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	private function getMap() {
		$map = $parameter = array();

		$groupid = input('param.groupid', '', 'intval');
		if ($groupid) {
			$map[] = ['groupid', '=', $groupid];
			$parameter['groupid'] = $groupid;
		}
		$this->assign('groupid', $groupid);

		$id = input('param.id', '', 'intval');
		if ($id) {
			$map[] = ['id', '=', $id];
			$parameter['id'] = $id;
		}
		$this->assign('id', $id);

		$username = input('param.username', '', 'pwEscape');
		if ($username) {
			$map[] = ['username', 'like', '%'.$username.'%'];
			$parameter['username'] = $username;
		}
		$this->assign('username', $username);

		$realname = input('param.realname', '', 'pwEscape');
		if ($realname) {
			$map[] = ['realname', 'like', '%'.$realname.'%'];
			$parameter['realname'] = $realname;
		}
		$this->assign('realname', $realname);

		$mobile = input('param.mobile', '', 'pwEscape');
		if ($mobile) {
			$map[] = ['mobile', 'like', '%'.$mobile.'%'];
			$parameter['mobile'] = $mobile;
		}
		$this->assign('mobile', $mobile);

		return array($map, $parameter);
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->userModel->getInfo($id);
		} else {
			$info = [
				'state' => 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);

		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->userModel->get($id);
		}

		//用户密码
		if ($data['password']) {
			$data['password'] = sp_password($data['password']);
		} else {
			unset($data['password']);
		}
		
		if ($id) {
			$result = $this->userModel->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->success('保存成功', url('admin/systemUser/index'));
			} else {
				$this->error('保存失败'.showDbError($this->userModel));
			}
		} else {
			$data['create_time'] = $this->nowtime;
			$result = $this->userModel->baseAddData($data);
			if ($result !== false) {
				$this->success('添加成功', url('admin/systemUser/index'));
			} else {
				$this->error('添加失败'.showDbError($this->userModel));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		if (!$id) {
			$this->error('非法ID');
		}

		$result = $this->userModel->del($id);
		if ($result !== false) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}

	/**
	 * ajax获取接口(列表)
	 */
	public function ajaxAssociate() {
		$map = [];
		$word = input('param.word', '', 'pwEscape');
		if ($word) {
			$map[] = ['username', 'like', '%'.$word.'%'];
		}
		list($list) = $this->userModel->search($map, '');
		$newList = [];
		foreach ($list as $key => $value) {
			$tmp = [];
			$tmp['id'] = $value['id'];
			$tmp['text'] = $value['username'];
			$newList[] = $tmp;
		}
		$resultData = [
			'list' => $newList
		];
		$this->jsonResult(200, '', $resultData);
	}

}